////////////////////////////////////////////////////////////////////////////////
// This file is part of the Mist compiler.                                    //
//                                                                            //
// Copyright (C) 2009 The Mist project                                        //
//                                                                            //
// For a list of contributors, see the COMMITTERS file.                       //
//                                                                            //
// The Mist compiler is free software: you can redistribute it and/or modify  //
// it under the terms of the GNU General Public License as published by       //
// the Free Software Foundation, either version 3 of the License, or          //
// (at your option) any later version.                                        //
//                                                                            //
// The Mist compiler is distributed in the hope that it will be useful,       //
// but WITHOUT ANY WARRANTY; without even the implied warranty of             //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              //
// GNU General Public License for more details.                               //
//                                                                            //
// You should have received a copy of the GNU General Public License          //
// in the COPYING file. If not, see <http://www.gnu.org/licenses/>.           //
////////////////////////////////////////////////////////////////////////////////

///
/// \file
/// Contains several handy functions related to numeric types.
///

#ifndef COMMON_NUMERIC_HPP
#define COMMON_NUMERIC_HPP

////////////////////////////////////////////////////////////////////////////////
// Includes                                                                   //
////////////////////////////////////////////////////////////////////////////////

#include <string>
using std::string;

#include "common/assert.hpp"
#include "common/types.hpp"

////////////////////////////////////////////////////////////////////////////////
// Function Declarations                                                      //
////////////////////////////////////////////////////////////////////////////////

/// \return the greater of the two parameters \a a and \a b
///
template <class T>
T max(T a, T b);

/// \return the lesser of the two parameters \a a and \a b
///
template <class T>
T min(T a, T b);

/// \return the amount of digits of \a value in base \a base
///
template <class T>
ushort digit_count(T value, ushort base = 10);

/// \return the ordinal indicator for \a number in the English language;
///         one of "st", "nd", "rd", "th"
///
string ordinal_indicator(uint number);

/// \return the ordinal notation for \a number in the English language;
///         one of "#st", "#nd", "#rd", "#th", where '#' is \a number
///
string ordinal(uint number);

////////////////////////////////////////////////////////////////////////////////
// Template Function Implementations                                          //
////////////////////////////////////////////////////////////////////////////////

template <class T>
T max(T a, T b) {
	return (b > a ? b : a);
}

template <class T>
T min(T a, T b) {
	return (b < a ? b : a);
}

template <class T>
ushort digit_count(T value, ushort base) {
	assert(value > 0);
	
	ushort result = 0;
	
	while (value > 0) {
		++result;
		value /= base;
	}
	
	return result;
}

#endif // COMMON_NUMERIC_HPP
